package com.kgc.util;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

import java.util.Random;

public class SendSmsUtil {
	public static void sendSms(String phoneNo, String checkCode){
		DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4FnvmK9t5grwmG6b2Thh", "BbYFvjtrjAWwKd63vqNgKw9OvNr1sw");
		IAcsClient client = new DefaultAcsClient(profile);

		CommonRequest request = new CommonRequest();
		request.setMethod(MethodType.POST);
		request.setDomain("dysmsapi.aliyuncs.com");
		request.setVersion("2017-05-25");
		request.setAction("SendSms");
		request.putQueryParameter("RegionId", "cn-hangzhou");
		request.putQueryParameter("PhoneNumbers", phoneNo);
		request.putQueryParameter("SignName", "柠檬云");
		request.putQueryParameter("TemplateCode", "SMS_185212866");
		request.putQueryParameter("TemplateParam", "{\"code\":" + checkCode + "}");
		try {
			CommonResponse response = client.getCommonResponse(request);
			System.out.println(response.getData());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		Random random = new Random();
		int checkCode = 0;
		// 程序自己生成随机的
		do {
			checkCode = random.nextInt(10000);
		} while(checkCode < 1000);
		SendSmsUtil.sendSms("16606261470",String.valueOf(checkCode));
	}
}
